<template>
  <div class="roleManagement">
    <el-card class="box-card">
      <el-form>
        <el-form-item>
          <el-input
            v-model="queryData.name"
            placeholder="角色名称"
            clearable
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-select v-model="queryData.state" clearable>
            <el-option label="启用" value="0"></el-option>
            <el-option label="禁用" value="1"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-input placeholder="角色描述" clearable></el-input>
        </el-form-item>
        <el-form-item>
          <el-button @click="serachBtn">查询</el-button>
          <el-button type="primary" @click="handlerAddBtn">新增</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <el-card class="box-card">
      <el-table v-loading="loading" :data="roleList" border style="width: 100%">
        <el-table-column prop="rname" label="名称" align="center">
        </el-table-column>
        <el-table-column prop="state" label="状态" align="center">
          <template slot-scope="scope">
            <div>{{ scope.row.state == 0 ? "启用" : "禁用" }}</div>
          </template>
        </el-table-column>
        <el-table-column prop="describe" align="center" label="描述">
        </el-table-column>
        <el-table-column align="center" label="操作">
          <template slot-scope="scope">
            <el-button
              type="text"
              size="small"
              @click="handlerEdit(scope.$index, scope.row)"
              >修改</el-button
            >
            <el-button
              type="text"
              size="small"
              @click="handlerDelete(scope.row)"
              >删除</el-button
            >
            <el-button
              type="text"
              v-show="scope.row.state == 0"
              size="small"
              @click="handlerForbidden(scope.row)"
              >禁用</el-button
            >
            <el-button
              type="text"
              v-show="scope.row.state !== 0"
              size="small"
              @click="handlerEnable(scope.row)"
              >启用</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <Pagination
        :total="total"
        :page.sync="queryData.pageNum"
        :limit.sync="queryData.pageSize"
        @pagination="getRole"
      />
    </el-card>
    <!-- 添加弹窗 -->
    <AddRole
      :addDialogVisible="addDialogVisible"
      @addRoleCancel="addRoleCancel"
      @addRole="addRole"
    />
    <!-- 修改弹窗 -->
    <EditRole
      :editDialogVisible="editDialogVisible"
      :editForm="editForm"
      @editRoleCancel="editRoleCancel"
      @editRole="editRole"
    />
  </div>
</template>
<script>
import AddRole from "./components/addRole.vue";
import EditRole from "./components/editRole.vue";
import Pagination from "../../components/Pagination/index.vue";
// 导入接口文件
import {
  getRoleList,
  deleteRole,
  addRole,
  updateRole,
  updateState,
} from "@/api/role";

export default {
  components: {
    AddRole,
    EditRole,
    Pagination,
  },
  data() {
    return {
      loading: true,
      total: 0, //总条数
      queryData: {
        pageNum: 1, //当前显示多少数据
        pageSize: 8, // 当前页
        name: "", //角色名称
        state: "", //状态
      }, //请求参数
      addDialogVisible: false,
      editDialogVisible: false,
      editForm: {},
      region: "",

      addRoleList: {
        rname: "", //角色名称
        code: "", //角色标识
        describe: "", //角色描述
        state: 0, //状态 0: 启用 1: 禁用
        id: "", //角色id
      },
      roleList: [],
    };
  },

  created() {
    this.getRole();
  },
  methods: {
    // 获取数据列表
    getRole() {
      getRoleList(this.queryData).then((res) => {
        if (res.status === 200) {
          this.loading = false;
          this.roleList = res.data.records; //列表数据
          this.total = res.data.total; //总条数
          this.queryData.pageNum = res.data.current; //当前页
          this.queryData.pageSize = res.data.size; //每页显示条数
        }
      });
    },
    // 打开新增弹窗按钮
    handlerAddBtn() {
      this.addDialogVisible = true;
    },
    // 取消新增按钮
    addRoleCancel(valFalse) {
      this.addDialogVisible = valFalse;
    },
    // 确认新增按钮
    addRole(valList, valFalse) {
      this.addDialogVisible = valFalse;
      addRole(valList).then(res => {
        if (res.status === 200) {
        this.getRole();
        this.$message({
          message: "新增角色成功",
          type: "success",
        });
      }
      });
      
    },
    // 打开修改弹窗按钮
    handlerEdit(index, row) {
      this.editDialogVisible = true;
      this.editForm = Object.assign({}, row); //将数据传入dialog页面
      this.editForm.index = index;
    },
    // 取消修改按钮
    editRoleCancel(valFalse) {
      this.editDialogVisible = valFalse;
    },
    // 确认修改按钮
    editRole(editForm, valFalse) {
      updateRole(this.editForm).then((res) => {
        if (res.status === 200) {
          this.getRole();
          this.$message({
            message: "修改角色成功",
            type: "success",
          });
        }
      });
      this.editDialogVisible = valFalse;
    },
    // 删除按钮
    handlerDelete(rows) {
      this.$confirm(
        "删除角色,用于该角色的账号将无法查看相关菜单页, 是否继续?",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(() => {
          deleteRole(rows.id).then((res) => {
            if (res.status === 200) {
              this.getRole();
              this.$message({
                type: "success",
                message: "删除成功!",
              });
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    // 禁用按钮
    handlerForbidden(rows) {
      this.$confirm("禁用该角色,相关账号登录只能查看首页", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          updateState({ id: rows.id }).then((res) => {
            if (res.status === 200) {
              this.getRole();
              this.$message({
                type: "success",
                message: "禁用成功!",
              });
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消禁用",
          });
        });
    },
    // 启用按钮
    handlerEnable(rows) {
      this.$confirm("启用该角色,相关账号登录可查看相应资源菜单", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          updateState({ id: rows.id }).then((res) => {
            if (res.status === 200) {
              this.getRole();
              this.$message({
                type: "success",
                message: "启用成功!",
              });
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消启用",
          });
        });
    },
    //点击查询
    serachBtn() {
      this.getRole();
    },
  },
};
</script>
<style lang="scss" scoped>
.el-card {
  margin: 20px;
  .el-input {
    width: 350px;
  }
  .el-form {
    display: flex;
    justify-content: space-evenly;
  }
}
</style>
